package com.example.application2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class StartServiceActivity extends AppCompatActivity {
    private static final String TAG = "TAG";

    ServiceConnection mServiceConnection;
    MyService mMyService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_service);
        mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.v(TAG, "onServiceConnected");
                mMyService = ((MyService.LocalBinder) iBinder).getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.v(TAG, "onServiceDisconnected");
            }
        };
    }

    public void onButtonStartServiceClick(View view) {
        Intent intent = new Intent(StartServiceActivity.this, MyService.class);
        bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE);
    }

    public void onButtonStopServiceClick(View view) {
        if (mMyService != null) unbindService(mServiceConnection);
    }

    public void onButtonAddClick(View view) {
        if (mMyService != null)
            Toast.makeText(StartServiceActivity.this, "Using service add:" + mMyService.add(10, 5), Toast.LENGTH_LONG).show();
    }

    public void onButtonSubClick(View view) {
        if (mMyService != null)
            Toast.makeText(StartServiceActivity.this, "Using service sub:" + mMyService.sub(10, 5), Toast.LENGTH_LONG).show();
    }

    public void onButtonMulClick(View view) {
        if (mMyService != null)
            Toast.makeText(StartServiceActivity.this, "Using service mul:" + mMyService.mul(10, 5), Toast.LENGTH_LONG).show();
    }

    public void onButtonDivClick(View view) {
        if (mMyService != null)
            Toast.makeText(StartServiceActivity.this, "Using service div:" + mMyService.div(10, 5), Toast.LENGTH_LONG).show();
    }
}